using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace ClickOnceMaster
{
    public class WizardPage : UserControl
    {
        #region Delegates

        private delegate void BindProcessor();

        #endregion

        #region Instance fields

        /// <summary>
        /// The indication which specifies whether the page has been loaded or not.
        /// </summary>
        private bool _isLoaded;

        private ErrorProvider _epValidator;

        private IContainer _components;

        #endregion

        #region .ctors

        public WizardPage()
        {
            InitializeComponent();
        }

        #endregion

        #region Instance properties

        #region UserControl overrides

        /// <summary>
        /// Gets or sets the wizard page title.
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Always)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public override sealed string Text
        {
            get { return base.Text; }
            set { base.Text = value; }
        }

        #endregion

        #region Virtual  properties

        /// <summary>
        /// Return the <see cref="Cursor"/> for whait state
        /// </summary>
        public virtual Cursor WhaitCursor
        {
            get { return Cursors.WaitCursor; }
        }

        #endregion

        #region General properties

        protected ErrorProvider Validator
        {
            get
            {
                return _epValidator;
            }
        }

        #endregion

        #endregion

        #region Instance methods

        #region UserControls override

        ///<summary>
        /// Overrides the <see cref="System.Windows.Forms.UserControl.OnLoad"></see> method.
        /// Prevents from overriding it by derived controls.
        ///</summary>
        ///<param name="e">An <see cref="T:System.EventArgs"></see> that contains the event data. </param>
        protected override sealed void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        #endregion

        #region General methods

        /// <summary>
        /// Loads page controls with appropriate data.
        /// </summary>
        public void LoadPage()
        {
            if (!_isLoaded)
            {
                LoadData();

                if (InvokeRequired)
                {
                    Invoke(new BindProcessor(BindPage));
                }
                else
                {
                    BindPage();
                }

                _isLoaded = true;
            }
        }

        /// <summary>
        /// Returns a boolean value which indicates whether the controls within the page filled up correctly by a user.
        /// </summary>
        /// <returns>If the page controls' values are valid returns <c>true</c>; otherwise - <c>false</c>.</returns>
        public virtual bool ValidatePage()
        {
            return true;
        }

        /// <summary>
        /// Loads data to the page.
        /// </summary>
        protected virtual void LoadData()
        {
        }

        /// <summary>
        /// Binds the page controls to the page data.
        /// </summary>
        protected virtual void BindPage()
        {
        }

        /// <summary>
        /// Reload data to the page.
        /// </summary>
        protected void ResetPage()
        {
            _isLoaded = false;
        }

        #endregion

        private void InitializeComponent()
        {
            this._components = new System.ComponentModel.Container();
            this._epValidator = new System.Windows.Forms.ErrorProvider(this._components);
            ((System.ComponentModel.ISupportInitialize)(this._epValidator)).BeginInit();
            this.SuspendLayout();
            // 
            // _epValidator
            // 
            this._epValidator.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.NeverBlink;
            this._epValidator.ContainerControl = this;
            // 
            // WizardPage
            // 
            this.Name = "WizardPage";
            ((System.ComponentModel.ISupportInitialize)(this._epValidator)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion
    }
}